<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="公司名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['companyName']" placeholder="请输入公司名称"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="VIN识别码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['vin']" placeholder="请输入VIN识别码"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="所属车队" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['fleet']" placeholder="请输入所属车队"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="运输内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['goods']" placeholder="请输入运输内容"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="行驶证" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple  v-decorator="['lisense']" ></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="环保清单" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple  v-decorator="['inventory']" ></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['status']" :trigger-change="true" dictCode="djzt" placeholder="请选择状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="排放类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['evnType']" :trigger-change="true" dictCode="pflx" placeholder="请选择排放类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车牌颜色" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['plateColor']" :trigger-change="true" dictCode="cpys" placeholder="请选择车牌颜色" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="身份证号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['idCard']" placeholder="请输入身份证号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="注册日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择注册日期" v-decorator="['registerDate']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['carType']" :trigger-change="true" dictCode="cllx" placeholder="请选择车辆类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="司机姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['driverName']" placeholder="请输入司机姓名"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发动机号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['engineNum']" placeholder="请输入发动机号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车牌号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['plateNo']" placeholder="请输入车牌号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="手机号码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['phoneNum']" placeholder="请输入手机号码"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="登记状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['statusLedger']" :trigger-change="true" dictCode="" placeholder="请选择登记状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="运输量" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['freightVolume']" placeholder="请输入运输量"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="机械型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['mechanicalModel']" placeholder="请输入机械型号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发动机型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['engineModel']" placeholder="请输入发动机型号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="机械环保代码/产品识别码(PIN)" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['pin']" placeholder="请输入机械环保代码/产品识别码(PIN)"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="使用性质" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['function']" :trigger-change="true" dictCode="function" placeholder="请选择使用性质" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="燃料类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['fuelType']" :trigger-change="true" dictCode="fuel_type" placeholder="请选择燃料类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆品牌型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['carBrandModel']" :trigger-change="true" dictCode="" placeholder="请选择车辆品牌型号" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆所有人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['vehicleOwner']" placeholder="请输入车辆所有人"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="车辆类别" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['carSort']" :trigger-change="true" dictCode="car_sort" placeholder="请选择车辆类别" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="整车(机)铭牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple  v-decorator="['mechineNameplate']" ></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="生产日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择生产日期" v-decorator="['producedTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发动机生产商" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['engineProducer']" placeholder="请输入发动机生产商"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="联网状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['networkingStatus']" :trigger-change="true" dictCode="" placeholder="请选择联网状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="环保登记编码" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['registrationCode']" placeholder="请输入环保登记编码"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="机械环保信息标签" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['machineLabel']" placeholder="请输入机械环保信息标签"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发动机铭牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-image-upload isMultiple  v-decorator="['engineNameplate']" ></j-image-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="运输货物名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['transportGoodsName']" placeholder="请输入运输货物名称"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="机械种类" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['machineryType']" placeholder="请输入机械种类"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="整车(机)铭牌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['machineNameplate']" placeholder="请输入整车(机)铭牌"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="开始时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择开始时间" v-decorator="['beginDate']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="结束时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择结束时间" v-decorator="['endDate']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="黑名单类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['blacklistType']" placeholder="请输入黑名单类型"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="数据类型（更新/添加）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['dateType']" placeholder="请输入数据类型（更新/添加）"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="操作状态（是否同步）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['operationalState']" placeholder="请输入操作状态（是否同步）"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'CarDataForm',
    components: {
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/CarData/carData/add",
          edit: "/CarData/carData/edit",
          queryById: "/CarData/carData/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'companyName','vin','fleet','goods','lisense','inventory','status','evnType','plateColor','idCard','registerDate','carType','driverName','engineNum','plateNo','phoneNum','statusLedger','freightVolume','mechanicalModel','engineModel','pin','function','fuelType','carBrandModel','vehicleOwner','carSort','mechineNameplate','producedTime','engineProducer','networkingStatus','registrationCode','machineLabel','engineNameplate','transportGoodsName','machineryType','machineNameplate','beginDate','endDate','blacklistType','dateType','operationalState'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'companyName','vin','fleet','goods','lisense','inventory','status','evnType','plateColor','idCard','registerDate','carType','driverName','engineNum','plateNo','phoneNum','statusLedger','freightVolume','mechanicalModel','engineModel','pin','function','fuelType','carBrandModel','vehicleOwner','carSort','mechineNameplate','producedTime','engineProducer','networkingStatus','registrationCode','machineLabel','engineNameplate','transportGoodsName','machineryType','machineNameplate','beginDate','endDate','blacklistType','dateType','operationalState'))
      },
    }
  }
</script>